package cn.jdy.plugins.admin;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import cn.jdy.authority.entity.Organ;
import cn.jdy.authority.entity.User;
import cn.jdy.authority.service.OrganService;
import cn.jdy.framework.log.BaseLog;
import cn.jdy.framework.util.DateUtil;
import cn.jdy.framework.util.StringUtil;
import cn.jdy.plugins.entity.Line;
import cn.jdy.plugins.service.LineService;
import cn.jdy.plugins.service.LiveService;

/**
 * 系统用户信息管理控制类 
 *
 * @author jga
 * @description: 针对线路统一管理的控制类。
 * @date 2017-07-28 10:10
 */
@Controller
@RequestMapping("/line")
public class LineCtrl {

	@Autowired
	protected HttpServletRequest request;
	@Autowired
	protected HttpSession session;
	// 自动注入用户信息管理业务处理操作接口
	@Autowired
	private LineService lineService;
	@Autowired
	private LiveService liveService;
	@Autowired
	private OrganService organ;

	/**
	 *  初始化
	 * @param entity
	 * @param fromDate
	 * @param toDate
	 * @param s
	 * @return
	 */
	@RequestMapping("/listin")
	public ModelAndView list(Line entity, String fromDate, String toDate) {
		ModelAndView mv = new ModelAndView("/platform/line.jsp");
		/*设置最高权限k*/
		User oper = (User) session.getAttribute("user");
		DetachedCriteria dc = DetachedCriteria.forClass(Organ.class);
	   	Organ bo=new Organ();
	   if(!oper.getUsername().equals("superadmin")){
		    bo.setPid("999999");
	   }
	   /*设置最高权限s*/
	    dc.add(Restrictions.like("pid", bo.getPid(),MatchMode.ANYWHERE));
		mv.addObject("classList",organ.find(dc));
	

		return mv;
	}
	/**
	 * 用户信息列表 异步数据查询
	 *
	 * @return Map<line> 用户信息(line)列表
	 * @description: 根据页面不同查询条件 获取系统用户列表信息
	 * @param: map 用户信息查询条件
	 * @param: curNo 列表查询当前页码
	 * @param: curSize 最大记录位置
	 * @author: jga
	 * @date: 2017-03-23 10:20
	 */
	@RequestMapping("/list")
	@ResponseBody
	public Map<String, Object> list(Line sp) {
		
		Map<String, Object> jsonMap = new HashMap<String, Object>();
		try {
		//	jsonMap.put("dataList", lineService.query(sp));
		//	jsonMap.put("iTotalDisplayRecords", lineService.count(sp));
		//	jsonMap.put("iTotalRecords", lineService.count());
			
			jsonMap.put("Rows", lineService.query(sp));
			jsonMap.put("Total", lineService.count(sp));
		} catch(Exception e) {
			BaseLog.e(this.getClass(), "查询异常", e);
		}
		return jsonMap;
	}

	

	/**
	 * 用户信息 新增或修改
	 *
	 * @return String status

	 * @author: jga
	 * @date: 2017-03-23 10:25
	 */
	@RequestMapping(value = "/addline", method = RequestMethod.POST)
	@ResponseBody
		public Map<String, Object> addline(Line sp) {
		ModelAndView mv = new ModelAndView();
		Map<String, Object> jsonMap = new HashMap<String, Object>();
		try {
		if(StringUtil.isEmpty(sp.getId())) {//
				sp.setCreateTime(DateUtil.getCurrentDate(DateUtil._DEFAULT2));
				this.lineService.save(sp);
			} else {//更新
				sp.setUpdateTime(DateUtil.getCurrentDate(DateUtil._DEFAULT2));
				this.liveService.updatemian(sp.getId(), sp.getUrl(), sp.getName());
				this.liveService.updateback(sp.getId(), sp.getUrl(), sp.getName());
				this.lineService.update(sp);
			}
    jsonMap.put("statusCode", 200);
		} catch(Exception e) {
      mv.addObject("statusCode", "500");
			//mv.setViewName(this.viewName.concat("4"));
			BaseLog.e(getClass(), "信息保存异常！", e);
		}

	   return jsonMap;
	}

	
	
	/**
	 * 修改初始化

	 * @description: 修改初始化
	
	 */
	@RequestMapping("/updLineInit")
	@ResponseBody
	public Line updLineInit(String id) {
		Line line = null;
		try {
			// 用户信息修改初始化
			line = lineService.findOne("id", id);
		} catch(Exception e) {
      BaseLog.e(this.getClass(), "updUserInit 修改运维人员前查询", e);
		}
		return line;
	}
	/**
	 *  删除
	 *
	 * @return Map 删除用户信息的成功、失败信息
	 * @description: 删除指定用户标识的系统用户信息, 对多个系统用户信息的删除操作
	 * @author: jga
	 * @date: 2017-03-23 16:40
	 */
	@RequestMapping("/delline")
	@ResponseBody
	public Map<String, Object> delUser(String userIds) {
		Map<String, Object> jsonMap = new HashMap<String, Object>();
		String backInfo = "";

		if("".equals(backInfo)) {
			backInfo = lineService.delUserById(userIds);
			jsonMap.put("message", backInfo);
		}
		return jsonMap;
	}
	
	/**
	 *  查询根据机构id查询，机构下面的线路
	 *
	 * type 等0 代表是主线路，1代表是备用线路
	 * @author: jga
	 * @date: 2017-03-23 16:40
	 */
	@RequestMapping("/selectzLine")
	public ModelAndView selectzLine(Line line,int type) {
		ModelAndView mv = new ModelAndView();
		String backInfo = "";

		if("".equals(backInfo)) {
		
			  DetachedCriteria  dc= DetachedCriteria.forClass(Line.class);
			  dc.add(Restrictions.eq("organid",line.getOrganid()));
			  dc.addOrder(Order.desc("createTime"));
			  List<Line> list=  lineService.find(dc);
				mv.addObject("datelist", list);

		}
		if(type==0){//0代表进去主线路ajax页面
			mv.setViewName("/platform/ajaxzLine.jsp");
		}else{
			mv.setViewName("/platform/ajaxbLine.jsp");
		}
		
		return mv;
	}

	

}
